package com.learn.project2.controller.wx.cart;

import com.learn.project2.bean.MarketUser;
import com.learn.project2.model.bo.wx.cart.*;
import com.learn.project2.model.vo.wx.auth.AuthLoginVO2;
import com.learn.project2.model.vo.wx.cart.CartCheckoutVO2;
import com.learn.project2.model.vo.wx.cart.CartIndexVO2;
import com.learn.project2.model.vo.wx.comment.CommentListVO2;
import com.learn.project2.service.wx.cart.CartService2;
import com.learn.project2.utils.Result;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: 尹圣淼
 * @Date: 2022/1/10 14:16
 */
@RestController
@RequestMapping("wx/cart")
public class CartController2 {

    @Autowired
    CartService2 cartService;

    /**
     * @description: 10. wx/cart/goodscount
     * @param:
     * @return:
     * @author 尹圣淼
     * @date: 2022-01-10 14:26
     */

    @RequestMapping(value = "goodscount", method = RequestMethod.GET)
    public Result goodsCount() {
        Object principal = SecurityUtils.getSubject().getPrincipal();
        if(principal == null){
            return Result.ok(0);
        }
        MarketUser marketUser = (MarketUser) principal;
        Integer id = marketUser.getId();
        return Result.ok(cartService.countGoods(id));
    }

    /**
     * @description: 11. wx/cart/add
     * @param:
     * @return:
     * @author 尹圣淼
     * @date: 2022-01-10 14:26
     */
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public Result add(@RequestBody CartAddBO2 cartAddBO) {

        Object principal = SecurityUtils.getSubject().getPrincipal();
        if(principal == null){
            return Result.fail(501,"请登录");
        }
        MarketUser marketUser = (MarketUser) principal;
        Integer id = marketUser.getId();
        // 需要在内部判断逻辑，若product表中数量不及购物车选中数量，则需要return fail报错。
        return cartService.addCart(cartAddBO,id);
    }

    /**
     * @description: 12. wx/cart/index
     * @param:
     * @return:
     * @author 尹圣淼
     * @date: 2022-01-10 14:24
     */

    @RequestMapping(value = "index", method = RequestMethod.GET)
    public Result index() {
       Object principal = SecurityUtils.getSubject().getPrincipal();
//        if(principal == null){
//            return Result.fail(501,"请先登录再访问。");
//        }
        MarketUser marketUser = (MarketUser) principal;
        Integer id = marketUser.getId();
        CartIndexVO2 cartIndexVO = cartService.getIndexVO(id);
        return Result.ok(cartIndexVO);
    }

    /**
     * @description: 13.wx/cart/update   点击编辑后进行更改。
     * @param:
     * @return:
     * @author 尹圣淼
     * @date: 2022-01-10 14:27
     */
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public Result update(@RequestBody CartUpdateBO2 cartUpdateBO) {
        // 在内部判断 如果超过数额 还是要报错；
        Object principal = SecurityUtils.getSubject().getPrincipal();
        if(principal == null){
            return Result.fail(501,"请先登录再访问。");
        }
        MarketUser marketUser = (MarketUser) principal;
        Integer id = marketUser.getId();
        return cartService.updateNum(cartUpdateBO,id);
    }

    /**
     * @description: 14. wx/cart/checked
     * @param:
     * @return:
     * @author 尹圣淼
     * @date: 2022-01-10 14:29
     */
    @RequestMapping(value = "checked", method = RequestMethod.POST)
    public Result checked(@RequestBody CartCheckedBO2 cartCheckedBO) {
        Object principal = SecurityUtils.getSubject().getPrincipal();
//        if(principal == null){
//            return Result.fail(501,"请先登录再访问。");
//        }
        MarketUser marketUser = (MarketUser) principal;
        Integer id = marketUser.getId();
        cartService.updateChecked(cartCheckedBO,id);
        return Result.ok(cartService.getIndexVO(id));
    }

    /**
     * @description: 15. wx/cart/fastadd
     * @param:
     * @return:
     * @author 尹圣淼
     * @date: 2022-01-10 14:29
     */
    @RequestMapping(value = "fastadd", method = RequestMethod.POST)
    public Result fastAdd(@RequestBody CartAddBO2 fastAddBO) {
        // 接收对象，返回cart_id
        Object principal = SecurityUtils.getSubject().getPrincipal();
        if(principal == null){
            return Result.fail(501,"请登录");
        }
        MarketUser marketUser = (MarketUser) principal;
        Integer id = marketUser.getId();
        Result result = cartService.fastAddCart(fastAddBO, id);
        return result;
    }

    /**
     * @description: 16. wx/cart/checkout
     * @param: cartId=0&addressId=21&couponId=0&userCouponId=37&grouponRulesId=0
     * @return:
     * @author 尹圣淼
     * @date: 2022-01-10 14:31
     */

    @RequestMapping(value = "checkout", method = RequestMethod.GET)
    public Result checkout(CartCheckoutBO2 cartCheckoutBO) {
        Object principal = SecurityUtils.getSubject().getPrincipal();
//        if(principal == null){
//            return Result.fail(501,"请先登录再访问。");
//        }
        MarketUser marketUser = (MarketUser) principal;
        Integer id = marketUser.getId();
        CartCheckoutVO2 cartCheckoutVO = cartService.getCheckoutVO(cartCheckoutBO,id);

        return Result.ok(cartCheckoutVO);
    }

    /**
     * @description: 17.wx/cart/delete  进入编辑后执行对购物车中 选中商品进行删除
     * @param:
     * @return:
     * @author 尹圣淼
     * @date: 2022-01-10 17:2,2
     */
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    public Result delete(@RequestBody CartDeleteBO2 cartDeleteBO) {
        Object principal = SecurityUtils.getSubject().getPrincipal();
//        if(principal == null){
//            return Result.fail(501,"请先登录再访问。");
//        }
        MarketUser marketUser = (MarketUser) principal;
        Integer id = marketUser.getId();
        cartService.deleteCart(cartDeleteBO,id);
        return Result.ok(cartService.getIndexVO(id));
    }
}
